<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="_token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="/assets/css/layui.css">
    <link rel="stylesheet" href="/assets/css/view.css"/>
    <title></title>
</head>
<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <form class="layui-form layui-card-body" method="post" action="">
                  @csrf
                  <div class="layui-form-item">
                    <label class="layui-form-label">课程名称</label>
                    <div class="layui-input-block">
                      <input type="text" name="course" required  lay-verify="required" placeholder="请输入课程名称" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">名师选择</label>
                    <div class="layui-input-block">
                      <select name="t_id" lay-filter="t_id" lay-verify="required">
                        @foreach($teachers as $v)
                          <option value="{{ $v->id }}" t_id="{{ $v->id }}">{{ $v->teacher }}</option>
                        @endforeach
                      </select>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">封面图</label>
                    <div class="layui-input-block">
                      <button type="button" class="layui-btn" id="coverimg">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                      </button>
                      <div class="layui-upload-list">
                        <!--预览图片-->
                        <img class="layui-upload-img" style="width:100px;" id="preview">
                        <!--提示上传信息-->
                        <p id="demoText"></p>
                      </div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-block">
                      <input type="number" name="price" required  lay-verify="required" placeholder="请输入课程价格" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <!-- <div class="layui-form-item">
                    <label class="layui-form-label">课程详情</label>
                    <div class="layui-input-block">
                      <textarea id="coursedetail" style="display: none;"></textarea>
                    </div>
                  </div> -->

                  <div class="layui-form-item">
                    <label class="layui-form-label">课程详情</label>
                    <div class="layui-input-block">
                      <script id="editor" name="detail" type="text/plain" style="width:100%;height:400px;"></script> 
                    </div>
                  </div>

                  <div class="layui-form-item">
                    <div class="layui-input-block">
                      <button class="layui-btn layui-btn-blue" lay-submit lay-filter="formDemo">立即提交</button>
                      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                  </div>
                </form>  
            </div>
        </div>
    </div>
</body>
<script src="/assets/layui.all.js"></script>
<script type="text/javascript" src="/lib/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="/lib/ueditor/1.4.3/ueditor.all.min.js"> </script>
<script type="text/javascript" src="/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
<script>
layui.use(['form', 'layer', 'laytpl','upload','layedit'], function(){
    var coverimg;
    var editIndex;
    var form = layui.form
      ,layer = layui.layer
      ,$=layui.jquery;

    $(function(){
        var ue = UE.getEditor('editor');
    });
    // var layedit = layui.layedit;
    //   layedit.set({
    //       uploadImage: {
    //         url: "{{route('admin.uploadedit')}}" //接口url
    //         ,type: 'post' //默认post
    //         ,headers: {
    //             'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    //         }
    //       }
    //   });
    // editIndex = layedit.build('coursedetail',{
    //   height: 535
    // }); //建立编辑器
    var upload = layui.upload;
    var uploadInst = upload.render({
        elem: '#coverimg' //绑定元素
        ,size:100
        ,headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        }
        ,url: "{{route('admin.upload')}}" //上传接口
        ,before: function(obj){//文件上传前的回调
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
            $('#preview').attr('src', result); //图片链接（base64）直接将图片地址赋值给img的src属性
          });
        }
        ,done: function(res){
          coverimg=res.data;
          //上传完毕回调
        }
        ,error: function(){
          //请求异常回调
          var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
              uploadInst.upload();
            });
        }
    });
    //监听提交
    form.on('submit(formDemo)', function(data){
        data.field.img=coverimg;
        // data.field.coursedetail = layedit.getContent(editIndex);
        $.ajax({   
          url:"{{route('admin.addcourse')}}",
          method:'post',
          data:data.field,
          dataType:'JSON',
          success:function(res){
            if(res.code=='0'){
              var findex = parent.layer.getFrameIndex(window.name);
              parent.location.reload();
              parent.layer.close(findex);
            }else{
              alert(res.msg);
            }
          },
          error:function (data) {
            layer.msg(JSON.stringify(data.field));
          }
        });
        return false;
    });
});
</script>
</html>